#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDataById_0x22_test.py
@Time    :   2024/09/03 13:31:28
@Author  :   yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import logging
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
import Set_up
import random
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


DID = [ #'''ECU DID'''
        # ECU生产日期 ECU Manufacture Date year-2bytes month-1bytes day-1bytes
        ('22 F1 8B','62 F1 8B 20 23 f6 f8'),
        # ECU序列号 ECU Serial Number 29bytes
        ('22 F1 8C', '62 F1 8C 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 12 13 14 15 00'),
        # 编程日期 Programming Date year-2bytes month-1bytes day-1bytes
        ('22 F1 99', '62 F1 99 20 24 09 23'),
        # ECU电源电压 ECU Power Voltage 2bytes
        ('22 12 60','62 12 60'), 
        # 车速 Vehicle Speed 2bytes
        ('22 12 61', '62 12 61'),
        # 点火状态 IGN Status 1byte
        ('22 12 62', '62 12 62'),
        # 整车档位 Gear Position 1byte
        ('22 12 63', '62 12 63'),
        # 雷达SoC温度 SoC temperature 2bytes
        ('22 12 64', '62 12 64'),
        # 系统状态 System State 1byte
        ('22 12 65', '62 12 65 01'),
        # 目标物坐标信息 Target Position 2bytes
        ('22 12 66', '62 12 66'),
        # 水平角度误差 Azimuth Offset 2bytes
        ('22 12 68', '62 12 68'),
        # 校准错误信息 EOL Error Status 1byte
        ('22 12 69', '62 12 69'),
        # 左右雷达传感器中心距离 Distance between sensor cencer_left and right 2bytes
        ('22 12 6A', '62 12 6A'),
        # 前后雷达传感器中心距离 Distance between sensor cencer_front and rear 2bytes
        ('22 12 6B', '62 12 6B'),
        # 车辆轴距 wheel base 2bytes
        ('22 12 6C', '62 12 6C'),
        # HW版本 ECU Supplier Hardware Version 8bytes
        ('22 FE 00', '62 FE 00 02 01 02 01'),
        # app版本 ECU Supplier application Software Version 8bytes
        ('22 FE 01', '62 FE 01' + Set_up.internalver1),
        # BOOT版本 ECU Supplier Bootloader1 Software Version 8bytes
        ('22 FE 02', '62 FE 02' + Set_up.internalver_boot1),
        # boot版本 ECU Supplier Bootloader2 Software Version 8bytes
        ('22 FE 03', '62 FE 03' + Set_up.internalver_boot2),
        # 校准版本 ECU Supplier Calibration Software Version 8bytes
        ('22 FE 04', '62 FE 04' + Set_up.calVer),
        # 校准版本 Current EDR Data 1308bytes
        ('22 10 00', '62 10 00'),
        # 校准版本 Previous EDR Data 1308bytes
        ('22 10 01', '62 10 01'),

        #'''system DID'''
        # VehicleIdentification(VIN) 车辆识别码 17bytes
        ('22 F1 90', '62 F1 90 01 01 31 32 33 34 35 36 37 38 39 01 01 01 01 01 01'),
        # 零部件号  PartNumber 9bytes
        ('22 F1 87', '62 F1 87' + Set_up.PartNumber1),
        # Bootloader软件版本号 BootloaderSoftwareVersionNumber
        ('22 F1 83', '62 F1 83' + Set_up.BootloaderSoftwareVersionNumber1),
        # 应用软件指纹信息 ApplicationSoftwareFingerprint year-1byte mpnth-1byte day-1byte testersn-6bytes
        ('22 F1 84', '62 F1 84 24 09 23 38 39 2e 30 30 31'),
        # 供应商代码 A755E02335 SupplierCode 10bytes
        ('22 F1 8A', '62 F1 8A' + Set_up.SupplierCode1),
        # ECU 硬件版本号 H001 ECUHardwareversionnumber 9bytes
        ('22 F1 91', '62 F1 91' + Set_up.HardwareVersionNumber1),
        # ECU 软件版本号 S006.001 ECUSoftwareversionnumber 9bytes
        ('22 F1 95', '62 F1 95' + Set_up.ECUSoftwareversionnumber1),   
    ]

# readDataByID_normal = [([0x22,0xF1,0x90],[0x62,0xF1,0x90,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01]),
#                        ([0x22,0xF1,0x87],[0x62,0xF1,0x87]+ Set_up.PartNumber),  # 零部件号  B00037861
#                        ([0x22,0xF1,0x83],[0x62,0xF1,0x83]+ Set_up.BootloaderSoftwareVersionNumber),  # Bootloader软件版本号
#                        ([0x22,0xF1,0x84],[0x62,0xF1,0x84]+ Set_up.ApplicationSoftwareFingerprint),  # 应用软件指纹信息
#                        ([0x22,0xF1,0x8A],[0x62,0xF1,0x8A]+ Set_up.SupplierCode),  # 供应商代码 A755E02335
#                        ([0x22,0xF1,0x91],[0x62,0xF1,0x91]+ Set_up.HardwareVersionNumber),  # ECU 硬件版本号 H001
#                        ([0x22,0xF1,0x95],[0x62,0xF1,0x95] + Set_up.ECUSoftwareversionnumber),  # ECU 软件版本号 S006.001
#                        ([0x22,0xF1,0x8B],[0x62,0xF1,0x8B,0x20,0x23,0xFF,0xFF]),  # ECU生产日期
#                        ([0x22,0xF1,0x8C],[0x62,0xF1,0x8C,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]),  # ECU序列号
#                        ([0x22,0xF1,0x99],[0x62,0xF1,0x99] + Set_up.ProgrammingDate),  # 编程日期
#                        ([0x22,0x12,0x60],[0x62,0x12,0x60,0x04,0xB6]),  # ECU电源电压
#                        ([0x22,0x12,0x61],[0x62,0x12,0x61,0x00,0x00]),  # 车速
#                        ([0x22,0x12,0x62],[0x62,0x12,0x62,0x02]),  # 点火状态
#                        ([0x22,0x12,0x63],[0x62,0x12,0x63,0x00]),  # 整车档位
#                        ([0x22,0x12,0x64],[0x62,0x12,0x64]),  # 雷达SoC温度[0x62,0x12,0x64,0x26,0xD4]
#                        ([0x22,0x12,0x65],[0x62,0x12,0x65,0x01]),
#                        ([0x22,0x12,0x66],[0x62,0x12,0x66,0x00,0x00,0x00,0x00]),  # 目标物坐标信息
#                        ([0x22,0x12,0x68],[0x62,0x12,0x68]),  # 水平角度误差
#                        ([0x22,0x12,0x69],[0x62,0x12,0x69]),  # 校准错误信息
#                        ([0x22,0x12,0x6A],[0x62,0x12,0x6A,0x00,0x00]),  # 左右雷达传感器中心距离
#                        ([0x22,0x12,0x6B],[0x62,0x12,0x6B,0x00,0x00]),  # 前后雷达传感器中心距离
#                        ([0x22,0x12,0x6C],[0x62,0x12,0x6C,0x00,0x00]),  # 车辆轴距
#                        ([0x22,0xFE,0x00],[0x62,0xFE,0x00,0x02,0x01,0x02,0x01]), # 2.1.2.1   HW版本
#                        ([0x22,0xFE,0x01],[0x62,0xFE,0x01]+Set_up.internalver), # 3.1.1.122 app版本
#                        ([0x22,0xFE,0x03],[0x62,0xFE,0x03,0x03,0x01,0x01,0x02,0x23,0x06,0x29,0x02]), # 3.1.1.2  boot版本
#                        ]

#档位名称 雷达返回 DBC信号
gear_map = [('P', '0x01', '0x01'),
            ('R', '0x02', '0x03'),
            ('N', '0x03', '0x02'),
            ('D', '0x04', '0x04'),
            # ('M', '0x0B', ''),
            # ('S', '0x0C', '0x07'),
            # ('E', '', '0x06'),
            ]


ReadDataById_NRC = [([0x22], [0x7F, 0x22, 0x13]),
                    ([0x22, 0xF1], [0x7F, 0x22, 0x13]),
                    ([0x22, 0x12], [0x7F, 0x22, 0x13]),
                    ([0x22, 0xF1, 0x87, 0x13], [0x7F, 0x22, 0x13]),
                    ([0x22, 0xF1, 0x8C, 0xFF], [0x7F, 0x22, 0x13]),
                    ([0x22, 0x12, 0x66, 0xDD, 0x14], [0x7F, 0x22, 0x13]),
                    ([0x22, 0xF1, 0x11, 0x34, 0xF0], [0x7F, 0x22, 0x13]),
                    ([0x22, 0xF1, 0x82], [0x7F, 0x22, 0x31]),
                    ([0x22, 0xFD, 0x07], [0x7F, 0x22, 0x31]),
                    ([0x22, 0xDD, 0x0F], [0x7F, 0x22, 0x31]),
                    ([0x22, 0x11, 0x22], [0x7F, 0x22, 0x31])
                    ]


class Test_ReadDataById_Phy(object):

    def ReadDID(self, Driver, req, resp, session):
        iStep = 1
        if req == '22 12 61':
            # 设置车速
            Driver['CAN'].set_signal_Speed(random.uniform(0, 255))
            time.sleep(0.5)
            msg = Driver['CAN'].read_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd')
            logging.info('speed is:{0}'.format(msg))
            time.sleep(0.5)
            resq = hex(int(msg/0.1))[2:]
            if len(resq) < 4:
                resq = (4-len(resq))*'0'+resq
            logging.info('22 12 61 speed is:{0}'.format(resq))
            resp = resp+resq
            # iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep)
            # Driver['CAN'].set_signal_Speed(0)
        if req == '22 12 62':
            # 设置点火状态
            for i in range(3):
                Driver['CAN'].set_signal_IGN(i)
                time.sleep(0.5)
                msg = Driver['CAN'].read_db_signal(Set_up.NetName, Set_up.nodeName, 'PEPS_0x1B1', 'PwrMod')
                resp = resp+hex(int(msg))[2:]
                iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep)
                resp = resp[:-1]
            Driver['CAN'].set_signal_IGN(2)

        if req == '22 12 63':
            # 设置挡位
            for i in range(1, 5):
                Driver['CAN'].set_signal_Gear(i)
                time.sleep(0.5)
                msg = Driver['CAN'].read_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION')
                logging.info(f'Gear is:{msg}')
                if int(msg) == 2:
                    resp = resp + '3'
                elif int(msg) == 3:
                    resp = resp + '2'
                
                else:
                    resp = resp + hex(int(msg))[2:]
                logging.info(f'{resp}')
                iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep)
                resp = resp[:-1]

        if req == '22 12 66':
            # Target Position
            Target_Position = random.randint(0, 180)
            resq = hex(int(Target_Position/0.01)+180)[2:]
            resq = resq.zfill(8)

            logging.info('22 12 66 Target_Position is:{0}'.format(resq))
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2e 12 66 ' + resq, '6e 12 66', 3, iStep, True)

        if req == '22 12 6A' or req == '22 12 6B' or req == '22 12 6C':
            Distance = random.uniform(0, 5)
            resq = hex(int(Distance/0.01))[2:]
            if len(resq) < 4:
                resq = (4-len(resq))*'0'+resq
            resp = resp+resq
            logging.info('22 {0} Distance is:{1}'.format(req[3:], resq))
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E'+req[3:]+resq, '6E'+req[3:], 3, iStep, True)

        if req == '22 F1 90':
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E F1 90 01 01 31 32 33 34 35 36 37 38 39 01 01 01 01 01 01", '6E F1 90', 3, iStep, True)

        if req == '22 F1 84' or req == '22 F1 99':
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E F1 84 24 09 23 38 39 2e 30 30 31", '6E F1 84', 2, iStep, True)
            time.sleep(0.5)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep)
        if req == '22 12 61' or req == '22 12 62' or req == '22 12 63':
            Driver['CAN'].set_signal_IGN(2)
            Driver['CAN'].set_signal_Gear(1)
            Driver['CAN'].set_signal_Speed(2)

    
    ids = [f'{req}' for req, resp in DID]
    @allure.title('Test {req} of read data by identifier 0x22')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_Default_Phy(self, action, Driver, req, resp):
        time.sleep(1)
        self.ReadDID(Driver, req, resp, 1)

    
    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.description('Test read data by DID of 0x22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_Extended_Phy(self, action, Driver, req, resp):
        self.ReadDID(Driver, req, resp, 3)

    def test_ReadDataById_Extended_More_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVolt(Driver, 11, 2, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 60 22 F1 99', '7F 22 13', 3, iStep, False)

    @allure.severity('normal')
    @allure.description('Test DID 12 60 of 0x22 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ReadDataById_1260_Default_Phy(self, Driver, action):
        iStep = 1
        iStep = Driver['CAN'].SetVolt(Driver, 11, 2, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 60', '62 12 60', 1, iStep)
        
        vltage = (uds_resp[3] << 8 | uds_resp[4])*0.01  
        with assume:assert 10.5 <= vltage <= 11.5, logging.error(f'correct vltage:[10.7 <= vltage <= 11.1], actual response:[{vltage}]')

        iStep = Driver['CAN'].SetVolt(Driver, 13, 3, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 60', '62 12 60', 1, iStep)
        vltage = (uds_resp[3] << 8 | uds_resp[4])*0.01
     
        with assume:assert 12.5 <= vltage <= 13.1, logging.error(f'correct vltage:[12.7 <= vltage <= 13.1], actual response:[{vltage}]')

    @allure.severity('normal')
    @allure.description('Test DID 12 61 of 0x22 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ReadDataById_1261_Default_Phy(self, Driver, action):
        iStep = 1
        with allure.step(f'Step{iStep}:Request:设置速度信号value为1'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 1)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 61', '62 12 61 00 0A', 1, iStep)

        with allure.step(f'Step{iStep}:Request:设置速度信号value为11.25'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 11.25)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 61', '62 12 61 00 70', 0, iStep)
        
        with allure.step(f'Step{iStep}:Request:设置速度信号value为0'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 0)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 61', '62 12 61 00 00', 0, iStep)

    ids = ['gear_message:{0}'.format(req) for req, resp, Gear_Position in gear_map]
    @allure.severity('normal')
    @allure.description('Test DID 12 63 of 0x22 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp,Gear_Position', gear_map, ids=ids)
    def test_ReadDataById_1263_Default_Phy(self, Driver, action, req, resp, Gear_Position):
        iStep = 1
        with allure.step(f'Step{iStep}:Request:读取当前档位'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSN_FLAG', 0)
            msg = Driver['CAN'].read_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION')
            iStep += 1

        iStep, uds_rep_default, size = Driver['CAN'].checkReqResp(Driver, '22 12 63', '62 12 63', 1, iStep)    
        
        with allure.step(f'Step{iStep}:Request:设置挡位信号{0}档'.format(req)):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSN_FLAG', 0)
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION', int(Gear_Position, 16))
            time.sleep(1)
            iStep += 1
        
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 63', '62 12 63', 0, iStep) 
       
        if uds_resp[3] != int(resp, 16):
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[3]), ' ')
            logging.error(f'correct response:uds_rep[3]:{int(resp,16)}, actual response:[{uds_resp[3]}]')
        with assume: assert uds_resp[3] == int(resp, 16)

        with allure.step(f'Step{iStep}:Request:设置挡位信号为初始档位'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSN_FLAG', 0)
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION', msg)
            time.sleep(1)
            iStep += 1
        
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 63', '62 12 63', 0, iStep) 

        if uds_resp != uds_rep_default:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp), ' ')
            logging.error(f'correct response:[0x62,0x12,0x63], actual response:[{actuRespStr}]')
        with assume: assert uds_resp == uds_rep_default

    @allure.severity('normal')
    @allure.description('Test DID 12 60 of 0x22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ReadDataById_1260_Extended_Phy(self, Driver, action):
        iStep = 1
        iStep = Driver['CAN'].SetVolt(Driver, 11, 2, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 60', '62 12 60', 3, iStep)
        
        vltage = (uds_resp[3] << 8 | uds_resp[4])*0.01  
        with assume:assert 10.5 <= vltage <= 11.1, logging.error(f'correct vltage:[10.7 <= vltage <= 11.1], actual response:[{vltage}]')

        iStep = Driver['CAN'].SetVolt(Driver, 13, 3, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 60', '62 12 60', 1, iStep)
        vltage = (uds_resp[3] << 8 | uds_resp[4])*0.01   
        with assume:assert 12.6 <= vltage <= 13.1, logging.error(f'correct vltage:[12.7 <= vltage <= 13.1], actual response:[{vltage}]')

    @allure.severity('normal')
    @allure.description('Test DID 12 61 of 0x22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ReadDataById_1261_Extended_Phy(self, Driver, action):
        iStep = 1
        with allure.step(f'Step{iStep}:Request:设置速度信号value为1'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 1)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 61', '62 12 61 00 0A', 3, iStep)

        with allure.step(f'Step{iStep}:Request:设置速度信号value为11.25'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 11.25)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 61', '62 12 61 00 70', 0, iStep)
        
        with allure.step(f'Step{iStep}:Request:设置速度信号value为0'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 0)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 61', '62 12 61 00 00', 0, iStep)

    ids = ['gear_message:{0}'.format(req) for req, resp, Gear_Position in gear_map]
    @allure.severity('normal')
    @allure.description('Test DID 12 63 of 0x22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp,Gear_Position', gear_map, ids=ids)
    def test_ReadDataById_1263_Extended_Phy(self, Driver, action, req, resp, Gear_Position):
        iStep = 1
        with allure.step(f'Step{iStep}:Request:读取当前档位'):
            msg = Driver['CAN'].read_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION')
            iStep += 1

        iStep, uds_rep_default, size = Driver['CAN'].checkReqResp(Driver, '22 12 63', '62 12 63', 3, iStep)    
        
        with allure.step(f'Step{iStep}:Request:设置挡位信号{0}档'.format(req)):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSN_FLAG', 0)
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION', int(Gear_Position, 16))
            time.sleep(2)
            iStep += 1
        
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 63', '62 12 63', 0, iStep) 
       
        if uds_resp[3] != int(resp, 16):
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[3]), ' ')
            logging.error(f'correct response:uds_rep[3]:{int(resp,16)}, actual response:[{uds_resp[3]}]')
        with assume: assert uds_resp[3] == int(resp, 16)

        with allure.step(f'Step{iStep}:Request:设置挡位信号为初始档位'):
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSN_FLAG', 0)
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION', msg)
            time.sleep(0.5)
            iStep += 1
        
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '22 12 63', '62 12 63', 0, iStep) 

        if uds_resp != uds_rep_default:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp), ' ')
            logging.error(f'correct response:[0x62,0x12,0x63], actual response:[{actuRespStr}]')
        with assume: assert uds_resp == uds_rep_default

    ids = ['Request:{0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ReadDataById_NRC]
    @allure.severity('normal')
    @allure.description('Test 0x22 NRC in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', ReadDataById_NRC, ids=ids)
    def test_ReadDataById_NRC_Default_Phy(self, Driver, action, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep) 

    ids = ['Request:{0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ReadDataById_NRC]
    @allure.severity('normal')
    @allure.description('Test 0x22 NRC in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', ReadDataById_NRC, ids=ids)
    def test_ReadDataById_NRC_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep) 


class Test_ReadDataById_Fun(object):

    ids = ['Request:{0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ReadDataById_NRC]
    @allure.severity('normal')
    @allure.description('Test 0x22 NRC in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', ReadDataById_NRC, ids=ids)
    def test_ReadDataById_NRC_Default_Fun(self, Driver, action, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun') 
